﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Configuration;
using System.Collections;

namespace Xmhouse.Passport
{
    /// <summary> 
    ///Public 的摘要说明
    /// </summary> 
    public class PassportManage
    {

       private Xmhouse.Data.SQLConn objData = new Xmhouse.Data.SQLConn("PassportConn");

        public string ErrInfo;

        private Xmhouse.Config.AppConfig.AppConfigInfo XCAIC = Xmhouse.Config.AppConfig.load();


        #region 登录
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pass"></param>
        /// <param name="strIp"></param>
        /// <param name="SessionId"></param>
        /// <param name="sValidCode"></param>
        /// <param name="iStaus"></param>
        /// <returns></returns>
        public int Login(string name, string pass, string strIp, string SessionId,string sValidCode, ref Passport Pa)
        {
            int iStaus = 500;
            try
            {
                Pa = new Passport();
                System.Data.SqlClient.SqlParameter[] pars = new System.Data.SqlClient.SqlParameter[6];
                pars[0] = new System.Data.SqlClient.SqlParameter("@UserName", name);
                pars[1] = new System.Data.SqlClient.SqlParameter("@PassWord", pass);
                pars[2] = new System.Data.SqlClient.SqlParameter("@IP", strIp);
                pars[3] = new System.Data.SqlClient.SqlParameter("@SessionId", SessionId);
                pars[4] = new System.Data.SqlClient.SqlParameter("@SiteID", 1);
                pars[5] = new System.Data.SqlClient.SqlParameter("@ActiveCode", sValidCode);
                System.Xml.XmlDocument XmlDoc = new System.Xml.XmlDocument();
                iStaus = objData.PrepareCommand("sp_PassportLogin", pars, ref XmlDoc);

                /* 数据库出错 */
                if (iStaus == -100)
                {
                    ErrInfo = objData.ErrInfo;
                    return iStaus;
                }

                //-2 激活码错误 ,-1 用户名密码出错,0 未审核/未激活,1 成功 , 2 冻结
                if (iStaus == 1 && XmlDoc!=null)
                {
                    Pa = (Passport)Xmhouse.Config.Serialization.DeSerialize(Pa.GetType(), XmlDoc.OuterXml);
                    Pa.IsLogin = true;
                }
                else
                {
                    Pa.IsLogin = false;
                }

            }
            catch (Exception ex)
            {
                Pa = new Passport();
                Pa.IsLogin = false;
                ErrInfo = ex.Message.ToString();
            }
            return iStaus;
        }
        #endregion

        #region 注册
        public int Register(string sName, string sPass, string sIP, string sTel, string sEmail, string sValiCode, string sAddress, string sEnrolnName, string sPrpedom, string sInitializeModel)
        {
            int iRows = -100;
            try
            {
                System.Data.SqlClient.SqlParameter[] pars = new System.Data.SqlClient.SqlParameter[11];
                pars[0] = new System.Data.SqlClient.SqlParameter("@rName", sName);
                pars[1] = new System.Data.SqlClient.SqlParameter("@rPassWord", sPass);
                pars[2] = new System.Data.SqlClient.SqlParameter("@rIP", sIP);
                pars[3] = new System.Data.SqlClient.SqlParameter("@rTel", sTel);
                pars[4] = new System.Data.SqlClient.SqlParameter("@rEmail", sEmail);
                pars[5] = new System.Data.SqlClient.SqlParameter("@rValiCode", sValiCode);
                pars[6] = new System.Data.SqlClient.SqlParameter("@SiteID", "1");
                pars[7] = new System.Data.SqlClient.SqlParameter("@EnrolnName", sEnrolnName);
                pars[8] = new System.Data.SqlClient.SqlParameter("@Address", sAddress);
                pars[9] = new System.Data.SqlClient.SqlParameter("@Prpedom", sPrpedom);
                pars[10] = new System.Data.SqlClient.SqlParameter("@InitializeModel", sInitializeModel);
                iRows = objData.PrepareCommand("P_MyRegister", pars);
            }
            catch (Exception ex)
            {
                string strEx = ex.Message.ToString();
                ErrInfo = objData.ErrInfo;
            }
            return iRows;
        }
        #endregion

        #region 退出登录
        //public void LoginOut()
        //{
        //    HttpCookie PassportCookie = INC.GetCookieObject(XCAIC.PassportCookie);
        //    if (PassportCookie != null)
        //    {
        //        PassportCookie.Expires = DateTime.Now.AddDays(-1);
        //        INC.SetCookie(PassportCookie);
        //        PassportCookie = null;
        //    }
        //    Pa = null;

        //}
        #endregion

        #region 发送信息
        /// <summary>
        /// 发送信息
        /// </summary>
        /// <param name="strMobilePhone">手机号码</param>
        /// <param name="strValiCode">激活码</param>
      
        public void SendMobilePhoneMssage(string strMobilePhone, string strValiCode)
        {
        
            try
            {
                //注册成功发送激活码
                string strSendReg = string.Empty;
                if (!string.IsNullOrEmpty(strMobilePhone))
                {
                    //string strSmsUser = XCAIC.SMSUser;
                    //string strSmsPwd = XCAIC.SMSPassword;
                    //string strSmsKey = XCAIC.SMSKey;
                    //string strSmsContent = XCAIC.SMSContent + "您的激活码是：" + strValiCode;
                    /// 以下是发送验证码短信
                    //SmsServer.Service1 objSms = new SmsServer.Service1();
                    //string strSmsID = objSms.SendMessages(strSmsUser, strSmsPwd, strMobilePhone, strSmsContent, "");              
                }
            }
            catch (Exception ex)
            {
                string strEx = ex.Message.ToString();
                ErrInfo = objData.ErrInfo;
            }
           
        }
        #endregion

        public  void Dispose()
        {
            objData = null;
            XCAIC = null;
        }

    }


    #region 通行证类

    [Serializable()]
    [XmlRoot("Passport")]
    public class Passport
    {
        public bool IsLogin = false;    //是否登录,由程序修改
        public int UserID;              //用户ID
        public int SiteID;              //站点ID
        public string UserName;         //用户名
        public string PassWord;         //用户密码
        public string State = "-1";     //状态（）

        public string InitModel;        //用户初期模块
        public string Prpedom;          //权限模块
        public long LoginTimes;         //登录次数
        public string LastLoginIP;      //是否登录IP
        public string LastLoginTime;    //最后登录时间
        public string StateTime;        //用户开始时间
        public string TermTime;         //用户到期时间
        public string Token="";            //主牌凭证

        public string AllMoney;         //总金额
        public string AllCredits;       //总积分 
        public string NickName;         //昵称
        public string Email;            //用户邮件


    }
    #endregion

    public interface IPassport
    {}

}